<template>
  <div class="container">
    <div class="headerbox" @mouseleave="onhandleEnd">
      <div class="header">
        <div class="headerlogo" @mouseover="onhandleEnd">
          <img src="@/assets/logo.png" alt="">
        </div>
        <ul>
            <li @click.stop="gohome('/home')" @mouseover="onhandleEnd" :class="state.path=='/home'? 'active':''">Home</li>
            <li @mouseover="onhandle('1')" :class="(state.path=='/jiaoyi/zhanghu' || state.path=='/jiaoyi/chanpin')? 'active':''">Trade<el-icon><CaretBottom /></el-icon></li>
            <li @mouseover="onhandle('2')" :class="(state.path.indexOf('/mt5')>-1)? 'active':''">MT5<el-icon><CaretBottom /></el-icon></li>
            <li @mouseover="onhandle('3')" :class="(state.path.indexOf('/news')>-1)? 'active':''">
              Learn Center<el-icon><CaretBottom /></el-icon>
            </li>
            <li @click.stop="gohome('/about')" @mouseover="onhandleEnd"  :class="state.path=='/about'? 'active':''">About Us</li>
        </ul>
        <div class="headerbtn" @mouseover="onhandleEnd">
          <span  @click="gohome('/openaccount')">Open Account</span>
          <span  @click="gohome('/login')">Login</span>
        </div>
      </div>
      <div class="menuitembox" v-show="state.show">
        <div>
          <div style="width:68px;"></div>
        <div style="flex:1;">
          <ul v-show="state.tabIndex=='1'">
            <li @click.stop="gohome('/jiaoyi/zhanghu')"  :class="state.path=='/jiaoyi/zhanghu'? 'active':''">
              Trade Account
            </li>
            <li @click.stop="gohome('/jiaoyi/chanpin')"  :class="state.path=='/jiaoyi/chanpin'? 'active':''">
              Trade Products
            </li>
          </ul>
          <ul v-show="state.tabIndex=='2'">
            <li @click.stop="gohome('/mt5/desktop')"  :class="state.path=='/mt5/desktop'? 'active':''">
              MT5 Desktop
            </li>
            <li @click.stop="gohome('/mt5/webtrader')"  :class="state.path=='/mt5/webtrader'? 'active':''">
              MT5 WebTrader
            </li>
            <li @click.stop="gohome('/mt5/mobile')"  :class="state.path=='/mt5/mobile'? 'active':''">
              MT5 Mobile
            </li>
          </ul>
          <ul v-show="state.tabIndex=='3'">
            <!-- /news -->
            <li  @click.stop="gohome('/news')"  :class="state.path=='/news'? 'active':''">
              NEWS
            </li>
          </ul>
        </div>
        <div style="width:190px;"></div>
        </div>
      </div>
    </div>
    <div class="mheaderbox">
        <div class="menuheaderlogo">
          <img src="@/assets/logo.png" alt="">
        </div>
        <div class="menuheaderbtn">
          <el-icon  @click="state.menushow=true;" :size="30" color="#6a6967"><Fold /></el-icon>
        </div>
      </div>
    <div class="body">
      <router-view />
    </div>
  </div>
  <div class="drawerbox">
    <el-drawer v-model="state.menushow" direction="ltr" size="100%">
      <template #header>
        <div class="menuheaderlogo">
          <img src="@/assets/logo.png" alt="">
        </div>
      </template>
      <template #default>
        <div>
          <el-menu
            :default-active="state.defaultActive"
            class="el-menu-vertical-demo"

          >
            <el-menu-item index="1" @click="gohome('/home')">
              <span>Home</span>
            </el-menu-item>
            <el-sub-menu index="2">
              <template #title>
                <span>Trade</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="2-1" @click="gohome('/jiaoyi/zhanghu')">Trade Account</el-menu-item>
                <el-menu-item index="2-2" @click="gohome('/jiaoyi/chanpin')">Trade Products</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>
            <el-sub-menu index="3">
              <template #title>
                <span>MT5</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="3-1"  @click="gohome('/mt5/desktop')">MT5 Desktop</el-menu-item>
                <el-menu-item index="3-2"  @click="gohome('/mt5/webtrader')">MT5 WebTrader</el-menu-item>
                <el-menu-item index="3-3"  @click="gohome('/mt5/mobile')">MT5 Mobile</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>
            <!-- <el-sub-menu index="4">
              <template #title>
                <span>Learn Center</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="4-1"  @click="gohome('/news')">NEWS</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu> -->
            <el-menu-item index="5" @click="gohome('/about')">
              <span>About US</span>
            </el-menu-item>
            <el-menu-item index="6" @click="gohome('/openaccount')">
              <span>Open account</span>
            </el-menu-item>
            <el-menu-item index="7" @click="gohome('/login')">
              <span>Login</span>
            </el-menu-item>
          </el-menu>
        </div>
      </template>
    </el-drawer>
  </div>
</template>
<script>
  import { defineComponent,onMounted,reactive,watch} from 'vue';
  import {useRouter , useRoute} from 'vue-router';
  export default defineComponent({

      setup() {
        const router = useRouter();
        const route = useRoute();
        let state = reactive({
          show:false,
          tabIndex:0,
          path:'/home',
          defaultActive:'1',
          menushow:false,
          paths:[
            {
              path:'/home',
              defaultActive:'1'
            },
            {
              path:'/jiaoyi/zhanghu',
              defaultActive:'2-1'
            },
            {
              path:'/jiaoyi/chanpin',
              defaultActive:'2-2'
            },
            {
              path:'/mt5/desktop',
              defaultActive:'3-1'
            },
            {
              path:'/mt5/webtrader',
              defaultActive:'3-2'
            },
            {
              path:'/mt5/mobile',
              defaultActive:'3-3'
            },
            {
              path:'/news',
              defaultActive:'4-1'
            },
            {
              path:'/about',
              defaultActive:'5' 
            }
          ]
        })

        onMounted(()=>{
          state.path = route.path;
          state.paths.forEach((item,index)=>{
            if(state.path.indexOf(item.path)>-1){
              state.defaultActive = item.defaultActive;
            }
          })
        })

        const onhandle=(data)=>{
          state.tabIndex = data;
          state.show = true;
        }

        const onhandleEnd=()=>{
          state.show = false;
        }

        const gohome=(path)=>{
          state.paths.forEach((item,index)=>{
            console.log(item,path)
            if(path.indexOf(item.path)>-1){
              state.defaultActive = item.defaultActive;
            }
          })
          router.push({
            path:path
          })

          state.menushow = false;
        }

        const handleClose=()=>{
          state.menushow = false;
        }

        watch(
          () => route.path,
          (newPath, oldPath) => {
            state.path = newPath;
            state.paths.forEach((item,index)=>{
              if(newPath.indexOf(item.path)>-1){
                state.defaultActive = item.defaultActive;
              }
            })
          }
        );

        return {
            state,
            onhandle,
            onhandleEnd,
            gohome,
            handleClose
        }
      }
  })
</script>
<style lang="scss" scoped>
  .container{
    position: relative;
    .headerbox{
      position: fixed;
      top: 0;
      left: 0;
      z-index: 99;
      width: 100%;
      background-color: #f9f6f2;
      height: 88px;
      box-shadow: 0 4rem 8rem 0 rgba(0, 0, 0, .07);

      .menuitembox{
        position: sticky;
        top:88px;
        left:0;
        right:0;
        background-color:#f9f6f2;
        height:106px;
        border-top:1px solid #6a6967;
        
        >div{
          max-width: 1600px;
          margin:0 auto;
          display: flex;
        flex-direction: row;
        align-items: center;
        }
        ul{
          display: flex;
          flex-direction: row;
          align-items: center;
          justify-content: flex-start;
          list-style: none;
          flex:1;
          height:106px;
          padding:0;
          gap: 40px;
          margin:0 auto;
          margin-left:240px;
          li{
            padding:8px;
            align-items: center;
            color: #6a6967;
            text-align: center;
            font-size: 16px;
            font-style: normal;
            font-weight: 400;
            line-height: 24px;
            transition: all .12s;
            cursor: pointer;
            position:relative;
          }
          li:hover{
            color: #E24737;
            cursor: pointer;
          }
          .active{
            color: #E24737;
          }
        }
      }
    }
    .mheaderbox{
      display: flex;
      flex-direction: row;
      align-items: center;
      position: fixed;
      top: 0;
      left: 0;
      z-index: 99;
      width: 100%;
      background-color: #f9f6f2;
      height: 60px;
      box-shadow: 0 4rem 8rem 0 rgba(0, 0, 0, .07);
      display:none;

      .menuheaderbtn{
        flex:1;
        text-align: right;
        padding-right:10px;
      }
    }
    .header{
      display: flex;
      flex-direction: row;
      align-items: center;
      margin: 0 auto;
      width: auto;
      height: 88px;
      max-width: 1600px;

      .headerlogo{
        width:68px;
        height:68px;
        img{
          width:68px;
          height:68px;
        }
      }

      ul{
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: flex-start;
        list-style: none;
        flex:1;
        margin:0 14px;
        padding:0;
        gap: 40px;
        margin-left:240px;

        li{
          padding:8px;
          align-items: center;
          color: #6a6967;
          text-align: center;
          font-size: 14px;
          font-style: normal;
          font-weight: 400;
          line-height: 24px;
          transition: all .12s;
          cursor: pointer;
          position:relative;
          display: flex;
          flex-direction: row;
          align-items: center;
        }
        li:hover{
          color: #E24737;
          cursor: pointer;
        }
        .active{
          color: #E24737;
        }
      }

      .headerbtn{
        display: flex;
        flex-direction: row;
        align-items: center;
        span{
          display: inline-block;
          padding:8px 16px;
          color: #9f734c;
          font-size: 14px;
          font-style: normal;
          font-weight: 400;
          line-height: 24px;
          position: relative;
          cursor: pointer;
        }
        span:before{
          content: "";
          display: inline-block;
          width: 1px;
          height: 20px;
          background-color: #9f734c;
          position: absolute;
          left: 0;
          top: 10px;
        }
        span:first-child:before{
          display: none; 
        }
      }
    }

    .body{
      
      
    }
  }
  :deep(.el-menu){
    border-right:none;
  }
  .menuheaderlogo{
    width:38px;
    height:38px;
    padding-left:10px;
    img{
      width:38px;
      height:38px;
    }
  }
  @media screen and (max-width: 1024px) {
     .headerbox{
      display: none!important;
     }
     .mheaderbox{
      display:flex!important;
     }
  }
</style>